/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/

package dBasea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;

import modeloa.Abokatu;

public class Abokatua {

	public List<String> getHiriak() throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izenaHiria FROM Hiria order by izenaHiria";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString(1));
		}
		rs.close();

		return x;
	}

	public JComboBox getAbokatuak(int idHiria) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izena FROM Abokatua where idHiria=" + idHiria;
		ResultSet rs = dbk.executeSQL(query);
		JComboBox x = new JComboBox();
		while (rs.next()) {
			x.addItem(rs.getString(1));
		}
		rs.close();

		return x;
	}

	public List<String> getAbokatuIzenak() throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izena FROM Abokatua";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString(1));
		}
		rs.close();

		return x;
	}

	public List<String> getAbokatuakHirian(int idItripu) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izena FROM Abokatua where idHiria=" + idItripu
				+ "";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString("izena"));
		}
		rs.close();

		return x;
	}

	public boolean existitzenDa(String idAbokatu) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT * FROM Abokatua WHERE idAbokatua='" + idAbokatu
				+ "'";
		ResultSet rs = dbk.executeSQL(query);
		boolean aurki = rs.next();

		return aurki;
	}

	public void aboSortu(String id, String izena, String abizenak,
			Integer telefonoa, String helbidea, String hiri)
			throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "INSERT INTO Abokatua (idAbokatua, izena, abizenak, telefonoa, helbidea, idHiria)"
				+ " VALUES ('"
				+ id
				+ "','"
				+ izena
				+ "', '"
				+ abizenak
				+ "' , "
				+ telefonoa
				+ ",'"
				+ helbidea
				+ "', (Select idHiria from Hiria where izenaHiria ='"
				+ hiri
				+ "'))";

		dbk.executeSQL(query);

	}

	public void aboEzabatu(String nif) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "DELETE FROM Abokatua WHERE idAbokatua='" + nif + "'";
		dbk.executeSQL(query);

	}

	public Abokatu getAbokatuDatuak(String id) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT * FROM Abokatua a, Hiria h WHERE idAbokatua='"
				+ id + "' and a.idHiria = h.idHiria";
		ResultSet rs = dbk.executeSQL(query);
		Abokatu abokatu = null;
		if (rs.next()) {
			String nif = rs.getString("idAbokatua");
			String izena = rs.getString("izena");
			String abizenak = rs.getString("abizenak");
			int telefonoa = rs.getInt("telefonoa");
			String helbidea = rs.getString("helbidea");
			String hiria = rs.getString("izenaHiria");
			abokatu = new Abokatu(nif, izena, abizenak, telefonoa, helbidea,
					hiria);
		}
		rs.close();

		return abokatu;
	}

	public void aboAldatu(String id, String izena, String abizenak,
			Integer telefonoa, String helbidea, String hiri)
			throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "UPDATE Abokatua SET izena='" + izena
				+ "', abizenak = '" + abizenak + "', telefonoa =" + telefonoa
				+ ", helbidea = '" + helbidea
				+ "',idHiria=(Select idHiria from Hiria where izenaHiria ='"
				+ hiri + "') WHERE idAbokatua = '" + id + "'";
		dbk.executeSQL(query);

	}

}
